﻿#region Header

/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.Ldoc.ViewModels.ToolBoxes
{
    using System;
    using System.Windows.Input;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Ldoc.ViewModels.Pages;
    using Probel.Ldoc.ViewModels.Properties;
    using Probel.Mvvm.DataBinding;

    public class CreateFolderViewModel : BaseViewModel
    {
        #region Fields

        private readonly ICommand cancelCommand;
        private readonly ICommand createFolderCommand;
        private readonly RecordEditorViewModel ParentViewModel;
        private readonly IPatientService Service;

        private string folderName = DateTime.Today.Year.ToString();

        #endregion Fields

        #region Constructors

        public CreateFolderViewModel(RecordEditorViewModel parentViewModel
            , IErrorService errorService
            , IPatientService service
            , bool isAsync = true)
            : base(errorService, isAsync)
        {
            this.ParentViewModel = parentViewModel;
            this.Service = service;

            this.cancelCommand = new RelayCommand(this.Cancel);
            this.createFolderCommand = new RelayCommand(this.CreateFolder, this.CanCreateFolder);
        }

        #endregion Constructors

        #region Properties

        public ICommand CancelCommand
        {
            get { return this.cancelCommand; }
        }

        public ICommand CreateFolderCommand
        {
            get { return this.createFolderCommand; }
        }

        public string FolderName
        {
            get { return this.folderName; }
            set
            {
                this.folderName = value;
                this.OnPropertyChanged(() => FolderName);
            }
        }

        #endregion Properties

        #region Methods

        private void Cancel()
        {
            this.ParentViewModel.UiMessage = UiMessageDto.Info(Messages.Msg_CancelFolderCreation);
        }

        private bool CanCreateFolder()
        {
            return !string.IsNullOrWhiteSpace(FolderName);
        }

        private void CreateFolder()
        {
            this.Async(() =>
            {
                var folder = new RecordDrawerCardDto() { Title = this.FolderName };
                this.Service.Create(folder);

            }, () => this.ParentViewModel.UiMessage = UiMessageDto.Info(Messages.Msg_CreatedFolder));
        }

        #endregion Methods
    }
}